import 'package:flutter/material.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/common/assets_utils.dart';
import 'package:fzcgx_app/provider/provider_widget.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_device_model.dart';
import 'package:fzcgx_app/ui/my/factory/view_model/factory_device_edit_view_model.dart';
import 'package:fzcgx_app/ui/widget/custom_appbar_widget.dart';

class FactoryDeviceEditPage extends StatefulWidget {
  FactoryDeviceEditPage({Key key, this.model,this.callback}) : super(key: key);
  final FactoryDeviceModel model;
  final Function callback;
  @override
  _FactoryDeviceEditPageState createState() => _FactoryDeviceEditPageState();
}

class _FactoryDeviceEditPageState extends State<FactoryDeviceEditPage> {
  FactoryDeviceEditViewModel viewModel;
  @override
  void initState() {
   viewModel=FactoryDeviceEditViewModel();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: CustomAppbar(
        title: widget.model.name,
        actions: <Widget>[
          InkWell(
            onTap: (){
              viewModel.postApi(context,widget.callback);
            },
              child: Container(
            alignment: Alignment.centerRight,
            margin: EdgeInsets.only(right: AdapterUtils.getWidth(15)),
            child: Text(
              '保存',
              style: TextStyle(
                fontSize: AdapterUtils.getSp(16),
                color: Color(0xff15A1FF),
                fontWeight: FontWeight.w500,
              ),
            ),
          ))
        ],
      ),
      body: SafeArea(
          child: ProviderWidget<FactoryDeviceEditViewModel>(
        model: viewModel,
        onReady: (viewModel) {
          viewModel.requestApi(widget.model.id);
        },
        builder: (context, viewModel, child) {
          return viewModel.state == ViewState.Success
              ? _buildSuccessWidget(viewModel)
              : Container();
        },
      )),
    );
  }

  Widget _buildSuccessWidget(FactoryDeviceEditViewModel viewModel) {
    return SingleChildScrollView(
        child: Container(
      margin: EdgeInsets.symmetric(horizontal: AdapterUtils.getWidth(15)),
      child: Column(
        children: <Widget>[
          SizedBox(
            height: AdapterUtils.getHeight(30),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: Color(0xffE8E9ED), width: 1))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                RichText(
                  text: TextSpan(children: <InlineSpan>[
                    TextSpan(
                        text: '品牌 ',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff606B79),
                          fontWeight: FontWeight.w500,
                        )),
                    TextSpan(
                        text: '*',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xffFF1515),
                          fontWeight: FontWeight.w500,
                        )),
                  ]),
                ),
                TextField(
                  controller: viewModel.brandController,
                  // keyboardType: TextInputType.numberWithOptions(),
                  style: TextStyle(
                    fontSize: AdapterUtils.getSp(18),
                    color: Color(0xff3B4859),
                    fontWeight: FontWeight.w500,
                  ),
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.only(
                          top: AdapterUtils.getHeight(10),
                          bottom: AdapterUtils.getHeight(20)),
                      hintText: '请输入品牌名',
                      border: InputBorder.none,
                      hintStyle: TextStyle(
                        fontSize: AdapterUtils.getSp(18),
                        color: Color(0xffD7D8DB),
                        fontWeight: FontWeight.w500,
                      )),
                )
              ],
            ),
          ),
          SizedBox(
            height: AdapterUtils.getHeight(24),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: Color(0xffE8E9ED), width: 1))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                RichText(
                  text: TextSpan(children: <InlineSpan>[
                    TextSpan(
                        text: '年限  ',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff606B79),
                          fontWeight: FontWeight.w500,
                        )),
                    TextSpan(
                        text: '*',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xffFF1515),
                          fontWeight: FontWeight.w500,
                        )),
                  ]),
                ),
                TextField(
                  controller: viewModel.yearController,
                  keyboardType: TextInputType.numberWithOptions(),
                  style: TextStyle(
                    fontSize: AdapterUtils.getSp(18),
                    color: Color(0xff3B4859),
                    fontWeight: FontWeight.w500,
                  ),
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.only(
                          top: AdapterUtils.getHeight(10),
                          bottom: AdapterUtils.getHeight(20)),
                      hintText: '请输入品牌年限 （年）',
                      border: InputBorder.none,
                      hintStyle: TextStyle(
                        fontSize: AdapterUtils.getSp(18),
                        color: Color(0xffD7D8DB),
                        fontWeight: FontWeight.w500,
                      )),
                )
              ],
            ),
          ),
          SizedBox(
            height: AdapterUtils.getHeight(24),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: Color(0xffE8E9ED), width: 1))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                RichText(
                  text: TextSpan(children: <InlineSpan>[
                    TextSpan(
                        text: '数量  ',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff606B79),
                          fontWeight: FontWeight.w500,
                        )),
                    TextSpan(
                        text: '*',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xffFF1515),
                          fontWeight: FontWeight.w500,
                        )),
                  ]),
                ),
                TextField(
                  controller: viewModel.numController,
                  keyboardType: TextInputType.numberWithOptions(),
                  style: TextStyle(
                    fontSize: AdapterUtils.getSp(18),
                    color: Color(0xff3B4859),
                    fontWeight: FontWeight.w500,
                  ),
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.only(
                          top: AdapterUtils.getHeight(10),
                          bottom: AdapterUtils.getHeight(20)),
                      hintText: '请输入（台）',
                      border: InputBorder.none,
                      hintStyle: TextStyle(
                        fontSize: AdapterUtils.getSp(18),
                        color: Color(0xffD7D8DB),
                        fontWeight: FontWeight.w500,
                      )),
                )
              ],
            ),
          ),
          SizedBox(
            height: AdapterUtils.getHeight(24),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border(
                    bottom: BorderSide(color: Color(0xffE8E9ED), width: 1))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                RichText(
                  text: TextSpan(children: <InlineSpan>[
                    TextSpan(
                        text: '图片   ',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xff606B79),
                          fontWeight: FontWeight.w500,
                        )),
                    TextSpan(
                        text: '*',
                        style: TextStyle(
                          fontSize: AdapterUtils.getSp(14),
                          color: Color(0xffFF1515),
                          fontWeight: FontWeight.w500,
                        )),
                  ]),
                ),
                Container(
                  height: AdapterUtils.getHeight(160),
                  padding: EdgeInsets.only(
                      top: AdapterUtils.getHeight(20),
                      bottom: AdapterUtils.getHeight(25)),
                  child: Row(
                    children: <Widget>[
                      viewModel.model.imgList.length == 0
                          ? InkWell(
                              onTap: () {
                                viewModel.selectPhoto(context);
                              },
                              child: Container(
                                child: Stack(
                                  children: <Widget>[
                                    AspectRatio(
                                        aspectRatio: 1.0,
                                        child: Image.asset(
                                            AssetsUtils.getAssetsImagesPath(
                                                'upload.png'),
                                            fit: BoxFit.cover)),
                                    Positioned(
                                        bottom: AdapterUtils.getHeight(17),
                                        child: Container(
                                          alignment: Alignment.center,
                                          width: AdapterUtils.getWidth(108),
                                          child: RichText(
                                            text:
                                                TextSpan(children: <InlineSpan>[
                                              TextSpan(
                                                  text: '上传图片（',
                                                  style: TextStyle(
                                                    fontSize:
                                                        AdapterUtils.getSp(12),
                                                    color: Color(0xff99A2AD),
                                                    fontWeight: FontWeight.w500,
                                                  )),
                                              TextSpan(
                                                  text: viewModel
                                                      .model.imgList.length
                                                      .toString(),
                                                  style: TextStyle(
                                                    fontSize:
                                                        AdapterUtils.getSp(12),
                                                    color: Color(0xff15A1FF),
                                                    fontWeight: FontWeight.w500,
                                                  )),
                                              TextSpan(
                                                  text: '/3）',
                                                  style: TextStyle(
                                                    fontSize:
                                                        AdapterUtils.getSp(12),
                                                    color: Color(0xff99A2AD),
                                                    fontWeight: FontWeight.w500,
                                                  )),
                                            ]),
                                          ),
                                        ))
                                  ],
                                ),
                              ),
                            )
                            //第一张图片
                          : Container(
                              child: Stack(
                                children: <Widget>[
                                  AspectRatio(
                                      aspectRatio: 1.0,
                                      child: Image.network(
                                        viewModel.model.imgList[0],
                                        fit: BoxFit.cover,
                                      )),
                                  Positioned(
                                      right: AdapterUtils.getWidth(6),
                                      top: AdapterUtils.getHeight(6),
                                      child: InkWell(
                                        onTap: () {
                                          viewModel.delPhoto( viewModel.model.imgList[0],);
                                        },
                                        child: Container(
                                          width: AdapterUtils.getWidth(24),
                                          height: AdapterUtils.getHeight(24),
                                          decoration: BoxDecoration(
                                              color:
                                                  Colors.black.withAlpha(400),
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(12))),
                                          child: Icon(
                                            Icons.close,
                                            color: Colors.white,
                                             size: 16,
                                          ),
                                        ),
                                      ))
                                ],
                              ),
                            ),
                      SizedBox(
                        width: AdapterUtils.getWidth(10),
                      ),
                      viewModel.model.imgList.length == 0
                          ? Container()
                          : viewModel.model.imgList.length == 1
                              ? InkWell(
                                  onTap: () {
                                    viewModel.selectPhoto(context);
                                  },
                                  child: Container(
                                    child: Stack(
                                      children: <Widget>[
                                        AspectRatio(
                                            aspectRatio: 1.0,
                                            child: Image.asset(
                                                AssetsUtils.getAssetsImagesPath(
                                                    'upload.png'),
                                                fit: BoxFit.cover)),
                                        Positioned(
                                            bottom: AdapterUtils.getHeight(17),
                                            child: Container(
                                              alignment: Alignment.center,
                                              width: AdapterUtils.getWidth(108),
                                              child: RichText(
                                                text: TextSpan(children: <
                                                    InlineSpan>[
                                                  TextSpan(
                                                      text: '上传图片（',
                                                      style: TextStyle(
                                                        fontSize:
                                                            AdapterUtils.getSp(
                                                                12),
                                                        color:
                                                            Color(0xff99A2AD),
                                                        fontWeight:
                                                            FontWeight.w500,
                                                      )),
                                                  TextSpan(
                                                      text: viewModel
                                                          .model.imgList.length
                                                          .toString(),
                                                      style: TextStyle(
                                                        fontSize:
                                                            AdapterUtils.getSp(
                                                                12),
                                                        color:
                                                            Color(0xff15A1FF),
                                                        fontWeight:
                                                            FontWeight.w500,
                                                      )),
                                                  TextSpan(
                                                      text: '/3）',
                                                      style: TextStyle(
                                                        fontSize:
                                                            AdapterUtils.getSp(
                                                                12),
                                                        color:
                                                            Color(0xff99A2AD),
                                                        fontWeight:
                                                            FontWeight.w500,
                                                      )),
                                                ]),
                                              ),
                                            ))
                                      ],
                                    ),
                                  ),
                                )
                                //第二张图片
                              : Container(
                                  child: Stack(
                                    children: <Widget>[
                                      AspectRatio(
                                          aspectRatio: 1.0,
                                          child: Image.network(
                                            viewModel.model.imgList[1],
                                            fit: BoxFit.cover,
                                          )),
                                      Positioned(
                                          right: AdapterUtils.getWidth(6),
                                          top: AdapterUtils.getHeight(6),
                                          child: InkWell(
                                            onTap: () {
                                              viewModel.delPhoto( viewModel.model.imgList[1]);
                                            },
                                            child: Container(
                                              width: AdapterUtils.getWidth(24),
                                              height:
                                                  AdapterUtils.getHeight(24),
                                              decoration: BoxDecoration(
                                                  color: Colors.black
                                                      .withAlpha(400),
                                                  borderRadius:
                                                      BorderRadius.all(
                                                          Radius.circular(12))),
                                              child: Icon(
                                                Icons.close,
                                                color: Colors.white,
                                                 size: 16,
                                              ),
                                            ),
                                          ))
                                    ],
                                  ),
                                ),
                      SizedBox(
                        width: AdapterUtils.getWidth(10),
                      ),
                      viewModel.model.imgList.length == 0
                          ? Container()
                          : viewModel.model.imgList.length == 1
                              ? Container()
                              : viewModel.model.imgList.length == 2
                                  ? InkWell(
                                      onTap: () {
                                        viewModel.selectPhoto(context);
                                      },
                                      child: Container(
                                        child: Stack(
                                          children: <Widget>[
                                            AspectRatio(
                                                aspectRatio: 1.0,
                                                child: Image.asset(
                                                  AssetsUtils
                                                      .getAssetsImagesPath(
                                                          'upload.png'),
                                                  fit: BoxFit.cover,
                                                )),
                                            Positioned(
                                                bottom:
                                                    AdapterUtils.getHeight(17),
                                                child: Container(
                                                  alignment: Alignment.center,
                                                  width: AdapterUtils.getWidth(
                                                      108),
                                                  child: RichText(
                                                    text: TextSpan(children: <
                                                        InlineSpan>[
                                                      TextSpan(
                                                          text: '上传图片（',
                                                          style: TextStyle(
                                                            fontSize:
                                                                AdapterUtils
                                                                    .getSp(12),
                                                            color: Color(
                                                                0xff99A2AD),
                                                            fontWeight:
                                                                FontWeight.w500,
                                                          )),
                                                      TextSpan(
                                                          text: viewModel.model
                                                              .imgList.length
                                                              .toString(),
                                                          style: TextStyle(
                                                            fontSize:
                                                                AdapterUtils
                                                                    .getSp(12),
                                                            color: Color(
                                                                0xff15A1FF),
                                                            fontWeight:
                                                                FontWeight.w500,
                                                          )),
                                                      TextSpan(
                                                          text: '/3）',
                                                          style: TextStyle(
                                                            fontSize:
                                                                AdapterUtils
                                                                    .getSp(12),
                                                            color: Color(
                                                                0xff99A2AD),
                                                            fontWeight:
                                                                FontWeight.w500,
                                                          )),
                                                    ]),
                                                  ),
                                                ))
                                          ],
                                        ),
                                      ),
                                    )
                                    //第三张图片
                                  : Container(
                                      child: Stack(
                                        children: <Widget>[
                                          AspectRatio(
                                              aspectRatio: 1.0,
                                              child: Image.network(
                                                viewModel.model.imgList[2],
                                                fit: BoxFit.cover,
                                              )),
                                          Positioned(
                                              right: AdapterUtils.getWidth(6),
                                              top: AdapterUtils.getHeight(6),
                                              child: InkWell(
                                                onTap: () {
                                                  viewModel.delPhoto(viewModel.model.imgList[2]);
                                                },
                                                child: Container(
                                                  width:
                                                      AdapterUtils.getWidth(24),
                                                  height:
                                                      AdapterUtils.getHeight(
                                                          24),
                                                  decoration: BoxDecoration(
                                                      color: Colors.black
                                                          .withAlpha(400),
                                                      borderRadius:
                                                          BorderRadius.all(
                                                              Radius.circular(
                                                                  12))),
                                                  child: Icon(
                                                    Icons.close,
                                                    color: Colors.white,
                                                     size: 16,
                                                  ),
                                                ),
                                              ))
                                        ],
                                      ),
                                    ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    ));
  }
}
